package com.ruoyi.system.service.impl;


import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DepInfo;
import com.ruoyi.system.mapper.DepInfoMapper;
import com.ruoyi.system.service.IDepInfoService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class DepInfoServiceImpl implements IDepInfoService {
    @Autowired
    private DepInfoMapper depInfoMapper;

    @Autowired
    private ISysDeptService deptService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertDepInfoBach(List<DepInfo> depList){
        int rows = 0;
        SysDept sysDept = null;
        List<SysDept> deptList = new ArrayList<>();
        for(DepInfo depInfo:depList){
            sysDept = new SysDept();
            sysDept.setDeptId(Long.parseLong(depInfo.getDeptId()));
            if(StringUtils.isNotNull(depInfo.getDepName())){
                sysDept.setDeptName(depInfo.getDepName());
            }else {
                sysDept.setDeptName("");
            }
            sysDept.setParentId(Long.parseLong(depInfo.getCgDeptParent()));
//            sysDept.setAncestors(getAncestors(depInfo.getDeptId(), depList));
            sysDept.setDeptPath(depInfo.getCgDeptPath());

            sysDept.setCreateBy("定时任务同步");
            if(StringUtils.equals(depInfo.getDeptId(),"100000")){
                sysDept.setParentId(0L);
            }
            if("A".equals(depInfo.getEffStatus())){
                sysDept.setStatus(UserConstants.DEPT_NORMAL);
            }else{
                sysDept.setStatus(UserConstants.DEPT_DISABLE);
            }
            if(StringUtils.isNotEmpty(depInfo.getCgDeptSort())){
                sysDept.setOrderNum(Integer.parseInt(depInfo.getCgDeptSort()));
            }
            sysDept.setIsCompany(depInfo.getCgIsCompany());
            deptList.add(sysDept);
        }
        rows += depInfoMapper.insertOrUpdateDept(deptList);
        return rows;
    }

    /**
     * 根据机构ID，获取该部门所有上级机构
     * @param deptId
     * @param depList
     * @return
     */
    public String getAncestors(String deptId, List<DepInfo> depList){
        if (StringUtils.isEmpty(deptId)) {//异常数据
            return "";
        }
        if (StringUtils.equals(deptId,"100000")) {
            return "100000";
        }else {
            String ancestors = deptId;
            for(DepInfo depInfo:depList){
                if(StringUtils.equals(depInfo.getDeptId(), deptId)) {
                    ancestors = getAncestors(depInfo.getCgDeptParent(), depList) + "," + ancestors;
                }
            }
            return  ancestors;
        }
    }

    public void sysMdmUpdateAncestors(){
        depInfoMapper.sysMdmUpdateAncestors();
    }

}
